<?php
namespace app\admin\controller;
use think\Db;
class Auser extends Base
{
    //欢迎页面
    public function wellcome()
    {
      $sql = "select
                  sum(hk_money) as hk_money,
                  sum(zc_money) as zc_money,
                  sum(dj_money) as dj_money,
                  sum(zz_money) as zz_money
              from
                  user
              ";
        $rs = Db::query($sql);
        return view('',$rs[0]);
    }

    //编辑资料
    function editself(){
      if(is_post()){
          $d = input();
          $rule = [
              'phone' =>  'regex:/^1\d{10}$/',
              'pwd'   =>  'min:6',
          ];
          $msg = [
              'phone.regex'   =>  '请输入正确的手机号',
              'pwd.min'  =>  '密码长度不能小于6位数',
          ];
          com_validate($rule, $msg, $d, true);

          unset($d['file']);
          if(empty($d['pwd'])){
            unset($d['pwd']);
          }else{
            $d['pwd'] = com_password($d['pwd']);
          }
          $d['id'] = $this->userinfo['id'];
          Db::name("admin_user")->update($d);
          com_check(['suc_msg'=>'修改成功!','suc_js'=>'alert']);
      }
      $info = $this->userinfo;
      return view('',['info'=>$info]);
    }

    #列表
    function lists(){
      if(is_post()){
          $cur_page = input('page');
          $keyword  = input('keyword');
          $min_time = input('min_time');
          $max_time = input('max_time');
          $sql = "
          select
              [1]
          from
              admin_user
          ";
          $param = [];
          $w = ['where'];
          if(!empty($min_time)){
              $w[] = ' and create_time>?';
              $param[] = strtotime($min_time);
          }
          if(!empty($max_time)){
              $w[] = ' and create_time<?';
              $param[] = strtotime($max_time);
          }
          if(!empty($keyword)){
              $w[] = ' and (username like ? or name like ?)';
              $param[] = "%{$keyword}%";
              $param[] = "%{$keyword}%";
          }
          #条件判断
          if (count($w)>1){
            $w[1] = str_replace("and","",$w[1]);
            $sql .= implode(" ",$w);
          }
          #分页
          $page_size = c('a.page_size');
          $page_html = com_list_page($sql, $param, $cur_page,$page_size);

          #内容
          $start = ($cur_page - 1) * $page_size;
          $field = "*";
          $sql_list = str_replace("[1]",$field,$sql);
          $sql_list .= " order by id desc limit {$start},{$page_size}";
          $rslist = Db::query($sql_list, $param);

          #部门
          $department = Db::query('select id,title from department');
          foreach ($rslist as $k => $v) {
              $rslist[$k]['create_time'] = com_formate_time($v['create_time']);
              $rslist[$k]['checkbox'] = com_checkbox($v['id']);

              #部门名称
              $rslist[$k]['department_ids'] = '';
              $ids = array_filter(explode(",",$v['department_ids']));
              foreach($department as $val){
                if(in_array($val['id'],$ids)){
                    $rslist[$k]['department_ids'] .= $val['title'].",";
                }
              }

              $rslist[$k]['action']  = com_btn_edit("/auser/edit/id/{$v['id']}","800px","400px");
              $rslist[$k]['action']  .= com_btn_del("/auser/delete/id/{$v['id']}", "您确定要删除【{$v['username']}】？");
          }
          $body_html = com_list_body($rslist, ['checkbox','username','name','department_ids','create_time','action']);
          com_check(['data'=>['com_body'=>$body_html, 'com_page'=>$page_html]]);
      }
      return view();
    }

    #添加
    function add(){
      if(is_post()){
            $d = input();
            $rule = [
                'username'=>'require|unique:admin_user',
                'department_ids'=>'require',
            ];
            $msg = [
                'department_ids.require'=>'请选择所属部门',
                'username.require'=>'请输入正确的手机号',
                'username.unique'=>'已存在的用户名',
            ];
            com_validate($rule, $msg, $d, true);
            $data = [
              'username'=>$d['username'],
              'name'=>$d['name'],
              'phone'=>$d['phone'],
              'department_ids'=> ",".implode(",",$d['department_ids']).",",
              'create_time'=>time(),
              'update_time'=>time(),
            ];
            if($d['pwd']){
              $data['pwd'] = com_password($d['pwd']);
            }
            Db::name('admin_user')->insert($data);
            com_check(['suc_msg'=>'添加成功!','suc_js'=>'alert|close|reload']);
      }
      $department = Db::query('select id,title from department');
      return view('',['department'=>$department]);
    }

    #编辑
    function edit($id=''){
      if(is_post()){
            $d = input();
            $rule = [
                'username'=>"require|unique:admin_user,username,{$id},id",
                'department_ids'=>'require',
                'pwd'      =>  'min:6',
            ];
            $msg = [
                'department_ids.require'=>'请选择所属部门',
                'username.require'=>'请输入正确的手机号',
                'username.unique'=>'已存在的用户名',
                'pwd.min' =>  '登陆密码至少6位数',
            ];
            com_validate($rule, $msg, $d, true);
            $data = [
              'id' => $d['id'],
              'username'=>$d['username'],
              'name'=>$d['name'],
              'phone'=>$d['phone'],
              'department_ids'=> $d['department_ids'] ? ",".implode(",",$d['department_ids']).",":",,",
              'create_time'=>time(),
              'update_time'=>time(),
            ];
            if($d['pwd']){
              $data['pwd'] = com_password($d['pwd']);
            }
            Db::name('admin_user')->update($data);
            com_check(['suc_msg'=>'编辑成功!','suc_js'=>'alert|close|reload']);
      }
      $info = Db::name('admin_user')->find($id);
      $department = Db::query('select * from department');
      if($info['department_ids']){
        $department_ids = array_filter(explode(",",$info['department_ids']));
      }else{
        $department_ids = [];
      }
      return view('',['info'=>$info,'department'=>$department,'department_ids'=>$department_ids]);
    }
    #删除
    function delete($id=''){
        Db::name('admin_user')->where("id",'neq',$this->userinfo['id'])->where('id','in',[$id])->delete();
        com_check(['suc_msg'=>'删除成功!','suc_js'=>'alert|close|reload']);
    }

    #批量删除
    function delall(){
      if(is_post()){
          $ids = input('ids/a');
          Db::name('admin_user')->where("id",'neq',$this->userinfo['id'])->where('id','in',$ids)->delete();
          com_check(['suc_msg'=>'删除成功!','suc_js'=>'alert|close|reload']);
      }
    }

}
